package com.example.tool.activtiy;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.PopupWindowCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.adapter.PopAdapter;
import com.example.tool.adapter.base.CommonAdapter;
import com.example.tool.adapter.base.CommonViewHolder;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.widget.PopUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created Date 2019/8/30
 *
 * @author liangkun
 * description：PopWindwon的案例
 * 资料：https://www.jianshu.com/p/6c32889e6377
 * https://github.com/zyyoona7/EasyPopup
 */
public class PopWindownActivity extends BaseActivity<CommonPresent> {

    @BindView(R.id.llMain)
    protected LinearLayout llMain;  @BindView(R.id.appbar)
    protected AppBarLayout barLayout;
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    @BindView(R.id.rv_pop)
    protected RecyclerView rvPop;
    private float mAlpha = 0.5f; //背景灰度  0-1  1表示全透明
    private String[] popStrings = {"左下出去", "居中居下", "右下对齐", "右下对齐", "easyPop中间弹窗", "easyPop底部弹窗",
            "easyPop大弹窗", "easyPop相对位置"};
    private PopAdapter popAdapter;
    private List<String> list = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.popwindow;
    }

    @Override
    protected void initView() {
        tvTitle.setText("POP专题");
        //这里的第二个参数6代表的是网格的列数
        rvPop.setLayoutManager(new GridLayoutManager(this, 2));
        popAdapter = new PopAdapter(this, new ArrayList<String>(), R.layout.pop_item);
        rvPop.setAdapter(popAdapter);




    }

    @Override
    protected void initData() {
        list.addAll(Arrays.asList(popStrings));
        popAdapter.updataData(list);
    }

    @Override
    protected void initEvent() {
        popAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, CommonViewHolder holder, int position) {
                switch (position) {
                    case 0:
                        PopUtil.getInstance().showLeftLagerPop(PopWindownActivity.this, llMain, "");
                        break;
                    case 1:

                        PopUtil.getInstance().showtop(PopWindownActivity.this, barLayout, "");
                        TestPopupWindow window = new TestPopupWindow(PopWindownActivity.this);
                        View contentView = window.getContentView();
                        //需要先测量，PopupWindow还未弹出时，宽高为0
                        contentView.measure(makeDropDownMeasureSpec(window.getWidth()),
                                makeDropDownMeasureSpec(window.getHeight()));
                        int offsetX = Math.abs(window.getContentView().getMeasuredWidth() - view.getWidth()) / 2;
                        int offsetY = 20;
                        PopupWindowCompat.showAsDropDown(window, view, offsetX, offsetY, Gravity.START);
                        break;
                    case 2:
                        TestPopupWindow window1 = new TestPopupWindow(PopWindownActivity.this);
                        View contentView1 = window1.getContentView();
                        //需要先测量，PopupWindow还未弹出时，宽高为0
                        contentView1.measure(makeDropDownMeasureSpec(window1.getWidth()),
                                makeDropDownMeasureSpec(window1.getHeight()));
                        int offsetX1 = view.getWidth() - window1.getContentView().getMeasuredWidth();
                        int offsetY1 = 20;
                        PopupWindowCompat.showAsDropDown(window1, view, offsetX1, offsetY1, Gravity.START);
                        break;
                    case 3:
                        break;
                    case 4:
                        PopUtil.getInstance().showCenterPop(PopWindownActivity.this, llMain, "我是中间弹窗");
                        break;
                    case 5:
                        PopUtil.getInstance().showBottomPop(PopWindownActivity.this, llMain, "我是中间弹窗");
                        break;
                    case 6:
                        PopUtil.getInstance().showTopLagerPop(PopWindownActivity.this, llMain, "");
                        break;
                    case 7:
                        PopUtil.getInstance().showRelativePop(PopWindownActivity.this, view, "");
//
                        break;
                }
            }

            @Override
            public boolean onItemLongClick(View view, CommonViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    protected CommonPresent createPresenter() {
        return null;
    }


    public class TestPopupWindow extends PopupWindow {

        public TestPopupWindow(Context context) {
            super(context);
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            setOutsideTouchable(true);
            setFocusable(true);
            setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            View contentView = LayoutInflater.from(context).inflate(R.layout.popup_test,
                    null, false);
            setContentView(contentView);
        }
    }

    /**
     * 让popwindow在上下左右显示
     *
     * @param measureSpec
     * @return
     */
    @SuppressWarnings("ResourceType")
    private static int makeDropDownMeasureSpec(int measureSpec) {
        int mode;
        if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) {
            mode = View.MeasureSpec.UNSPECIFIED;
        } else {
            mode = View.MeasureSpec.EXACTLY;
        }
        return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode);
    }

    /**
     * 窗口显示，窗口背景透明度渐变动画
     */
    private void showBackgroundAnimator() {
        if (mAlpha >= 1f) return;
        ValueAnimator animator = ValueAnimator.ofFloat(1.0f, mAlpha);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float alpha = (float) animation.getAnimatedValue();
                setWindowBackgroundAlpha(alpha);
            }
        });
        animator.setDuration(360);
        animator.start();
    }

    /**
     * 控制窗口背景的不透明度
     */
    private void setWindowBackgroundAlpha(float alpha) {
        if (mContext == null) return;
        if (mContext instanceof Activity) {
            Window window = ((Activity) mContext).getWindow();
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.alpha = alpha;
            window.setAttributes(layoutParams);
        }
    }
}
